#include common_scripts\utility;
#include maps\mp\_utility;
#include maps\mp\gametypes\_hud_util;
#include z_mod\utility;


init()
{
    precacheShader("hint_health");
    
    level.zMain_LastTroubleOrigin = (0, 0, 0);
    level.zMain_LastTroubleTime = 0;
    
    level thread onPlayerConnected();
}

onPlayerConnected()
{
    for(;;) {
        level waittill("connected", player);
        
        // player vars
        player.zClass_Building = undefined;
        player.zClass_CanChange = true;
        player.zClass_Current = -1;
        player.zClass_Wanted = 0;
        player.zClass_PrimaryWeapon = "";
        player.zClass_SecondaryWeapon = "";
        //player.zClass_PrimaryOffhand = "";
        //player.zClass_SecondaryOffhand = "";
        player.zHealth_ProcessHealing = false;
        player.zMain_AtBase = false;
        player.zMenu_UsingClassMenu = false;
        player.zMenu_UsingWeaponMenu = false;
        player.zMenu_UsingWeaponMenuDelay = false;
        player.zTeleport_CanUse = true;
        player.zWeapon_Primary = "";
        player.zWeapon_Secondary = "";
        player.zWeapon_Wanted = 0;
        player.zWeapon_RPGAmmo = 0;
        
        // player client dvars
        self setClientDvar("ui_drawcrosshair", 1);
        
        // player default threads (for [re]setting some vars)
        player thread onPlayerDied();
        player thread onPlayerSpawned();
        
        // player custom threads
        player thread zHud_Health();
        player thread zHud_TabStat();
        
        // player functions (e.g. notifyOnPlayerCommand())
        player notifyOnPlayerCommand("zkey_actionslot_2", "+actionslot 2"); // меню - отркыть/закрыть
        player notifyOnPlayerCommand("zkey_actionslot_4", "+actionslot 4"); // начать построение предмета
        player notifyOnPlayerCommand("zkey_actionslot_4_stop", "-actionslot 4"); // остановить построение предмета
        player notifyOnPlayerCommand("zkey_activate", "+activate"); // меню - применить
        player notifyOnPlayerCommand("zkey_attack", "+attack");
        player notifyOnPlayerCommand("zkey_back", "+back"); // меню - вниз
        player notifyOnPlayerCommand("zkey_forward", "+forward"); // меню - вверх
        player notifyOnPlayerCommand("zkey_scores", "+scores");
        player notifyOnPlayerCommand("zkey_scores_stop", "-scores");
    }
}

onPlayerDied()
{
    self endon("disconnect");
    
    for(;;) {
        self waittill("death");
        
        self.zHealth_ProcessHealing = false;
    }
}

onPlayerSpawned()
{
    self endon("disconnect");
    
    for(;;) {
        self waittill("spawned_player");
        
        self.zClass_CanChange = true;
        self.zMain_AtBase = true;
        self.zMenu_UsingClassMenu = false;
        self.zMenu_UsingWeaponMenu = false;
        self.zMenu_UsingWeaponMenuDelay = false;
        self.zTeleport_CanUse = true;
        
        self thread onPlayerShooted();
        self thread onPlayerTriedToGetToTheChoppa();
    }
}

onPlayerShooted()
{
    self endon("death");
    self endon("disconnect");
    
    for(;;) {
        self waittill("zkey_attack");
        
        if(!isSubStr(self getCurrentWeapon(), "silencer")) {
            level.zMain_LastTroubleOrigin = self.origin;
            level.zMain_LastTroubleTime = getTime();
        }
    }
}

onPlayerTriedToGetToTheChoppa()
{
    self endon("death");
    self endon("disconnect");
    
    for(;;) {
        self waittill("zkey_activate");
        
        if(!self.zMenu_UsingClassMenu && !self.zMenu_UsingWeaponMenu)
            level notify("zplayer_wants_choppa", self);
    }
}

zHud_Health()
{
    self endon("disconnect");
    
    self.zHud_Health = self createIcon("hint_health", 32, 32);
    self.zHud_Health.alpha = 0;
    self.zHud_Health.color = (1, 1, 1);
    self.zHud_Health.foreGround = true;
    self.zHud_Health.hideWhenInMenu = true;
    self.zHud_Health setPoint("BOTTOM LEFT", "BOTTOM LEFT", 5, -5);
    
    result = "";
    
    for(;;) {
        result = self waittill_any_return("spawned_player", "death");
        
        if(result == "spawned_player" && self.zHud_Health.alpha == 0) {
            self.zHud_Health.alpha = 1;
            
            self thread zHud_HealthUpdate(self.zHud_Health);
        } else if(result == "death" && self.zHud_Health.alpha == 1) {
            self.zHud_Health.alpha = 0;
        }
        
        wait(0.01);
    }
}

zHud_HealthUpdate(hud_elem)
{
    self endon("death");
    self endon("disconnect");
    
    for(;;) {
        hud_elem.color = (1.0, self.health/self.maxhealth, self.health/self.maxhealth);
        
        wait(0.25);
        //self waittill("damage");
    }
}

zHud_TabStat()
{
    self endon("disconnect");
    
    self.zHud_TabStat = [];
    
    self.zHud_TabStat[0] = self createFontString("hudbig", 0.75);
    self.zHud_TabStat[0].alpha = 0;
    self.zHud_TabStat[0].foreGround = true;
    self.zHud_TabStat[0].hideWhenInMenu = false;
    self.zHud_TabStat[0] setPoint("BOTTOM LEFT", "BOTTOM LEFT", 5, -15 - level.zClass_List.size*10);
    self.zHud_TabStat[0] setText("Busy classes:");
    
    for(i = 0; i < level.zClass_List.size; i++) {
        self.zHud_TabStat[i+1] = self createFontString("default", 1);
        self.zHud_TabStat[i+1].alpha = 0;
        self.zHud_TabStat[i+1].foreGround = true;
        self.zHud_TabStat[i+1].hideWhenInMenu = false;
        self.zHud_TabStat[i+1] setParent(self.zHud_TabStat[0]);
        self.zHud_TabStat[i+1] setPoint("BOTTOM LEFT", "BOTTOM LEFT", 5, 20 + i*10);
        //self.zHud_TabStat[i+1] setText(level.zClass_List[i].name + ": ");
    }
    
    result = "";
    
    for(;;) {
        result = self waittill_any_return("zkey_scores", "zkey_scores_stop");
        
        if(result == "zkey_scores" && self.zHud_TabStat[0].alpha == 0 && (isAlive(self) || self.pers["team"] == "spectator")) {
            for(i = 0; i < self.zHud_TabStat.size; i++) {
                self.zHud_TabStat[i].alpha = 1;
                
                if(i > 0) {
                    if(isDefined(level.zClass_List[i-1].busy)) {
                        self.zHud_TabStat[i] setText(level.zClass_List[i-1].name + ": ^1" + level.zClass_List[i-1].busy.name);
                    } else {
                        self.zHud_TabStat[i] setText(level.zClass_List[i-1].name + ": --");
                    }
                }
            }
        } else if(result == "zkey_scores_stop" && self.zHud_TabStat[0].alpha == 1) {
            for(i = 0; i < self.zHud_TabStat.size; i++)
                self.zHud_TabStat[i].alpha = 0;
        }
    }
}

